package hu.fic.weddingstorefinder.activity;

import hu.fic.weddingstorefinder.R;
import hu.fic.weddingstorefinder.db.WeddingStoreDBAdapter;
import hu.fic.weddingstorefinder.db.table.Dress;
import hu.fic.weddingstorefinder.db.table.POI;
import hu.fic.weddingstorefinder.listadapter.DressListAdapter;
import hu.fic.weddingstorefinder.listadapter.DressListSectionHeader;
import hu.fic.weddingstorefinder.listadapter.ListItem;
import hu.fic.weddingstorefinder.parcel.DressParcelable;
import hu.fic.weddingstorefinder.parcel.StoreParcelable;
import hu.fic.weddingstorefinder.utils.Constants;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class DressListActivity extends Activity implements OnItemClickListener {
	private ArrayList<ListItem> itemList = new ArrayList<ListItem>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.dresslist_layout);
		ListView dressListView = (ListView)findViewById(R.id.dresslistview);
		try {
			POI poi = new POI(WeddingStoreDBAdapter.getInstance().getDb(), this);
			String sql = "SELECT * FROM POI p, Dress d WHERE p.CustomPOIId = d.CustomPOIId ORDER BY p.POIName";
			poi.load(sql);
			String poiName = "";
			while (poi.nextRow()) {
				if (!poiName.equals(poi.getString(POI.COL_POINAME))) {
					DressListSectionHeader sectionHeader = new DressListSectionHeader(poi.getString(POI.COL_POINAME));
					itemList.add(sectionHeader);
					
					poiName = poi.getString(POI.COL_POINAME);
				}
				DressParcelable dressParcel = new DressParcelable();
				dressParcel.setDressDesc(poi.getString(Dress.COL_DRESSDESC));
				dressParcel.setCustomDressId(poi.getInt(Dress.COL_CUSTOMDRESSID));
				dressParcel.setCustomPOIId(poi.getInt(POI.COL_CUSTOMPOIID));
				dressParcel.setCurrency(poi.getString(Dress.COL_CURRENCY));
				dressParcel.setPrice(poi.getInt(Dress.COL_PRICE));
				dressParcel.setImageName(poi.getString(Dress.COL_IMAGENAME));
				dressParcel.setIsAvailable(poi.getInt(Dress.COL_ISAVAILABLE));
				itemList.add(dressParcel);
			}
			DressListAdapter dressAdapter = new DressListAdapter(itemList);
			dressListView.setAdapter(dressAdapter);
			dressListView.setOnItemClickListener(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		try {
			DressParcelable dressParcelable = (DressParcelable)itemList.get(position);
			Intent intent = new Intent(this, DressDetailsActivity.class);
			intent.putExtra(Constants.DRESS_PARCEL_KEY, dressParcelable);
			this.startActivity(intent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	

}
